package com.ruoyi.common.enums.business;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum CouponTypeEnum {

    PHYSICAL(1, "实物商品兑换券"),
    VIRTUAL(2, "虚拟商品兑换券"),
    SERVICE(3, "服务类兑换券");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, CouponTypeEnum> ENUM_MAP;

    static {
        CouponTypeEnum[] values = values();
        Map<Integer, CouponTypeEnum> map = new HashMap<>(values.length);
        for (CouponTypeEnum value : values) {
            map.put(value.code, value);
        }
        ENUM_MAP = Collections.unmodifiableMap(map);
    }

    /**
     * 根据code查找枚举
     */
    public static CouponTypeEnum getEnumByCode(Integer code) {
        CouponTypeEnum couponTypeEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(couponTypeEnum)) {
            throw new RuntimeException("无效的券类型编码: " + code);
        }
        return couponTypeEnum;
    }

    /**
     * 获取描述
     */
    public static String getDescByCode(Integer code) {
        CouponTypeEnum couponTypeEnum = ENUM_MAP.get(code);
        return ObjectUtil.isEmpty(couponTypeEnum) ? "未知类型" : couponTypeEnum.getDesc();
    }


}
